Skip to content

Conversation

@shawnbot
Copy link
Contributor

@shawnbot shawnbot commented Sep 12, 2017

This adds a helper function that can import Storybook stories from fenced code blocks in markdown files. It's surprisingly easy, thanks to remark and unist plugins!

JS

In a module's stories.js or stories/*.js file:

import React from 'react'
import { storiesOf } from '@storybook/react'
import storiesFromMarkdown from '../../.storybook/lib/storiesFromMarkdown'

const stories = storiesOf('My module name', module)

// this will grab all of the .md files; you can tighten the pattern for each module
storiesFromMarkdown(require.context('.', true, /\.md$/))
  .forEach(({title, story}) => {
    stories.add(title, story)
  })

Note: require.context() is a webpack thing that facilitates dynamic require() statements. I've added the raw-loader per these instructions, which is how webpack is able to read the markdown files as strings when you do something like require('README.md').

Markdown

In your README.md or docs/*.md, write fenced code blocks with the html lang and optional metadata in key-value pairs. The following rules apply:

  • Code blocks with story=false (or story="false") will be ignored.

  • The title metadata can be used to provide an explicit title for each code block:

    ```html title="this is my story title"
    story here
    ```
  • Otherwise, the text of the preceding heading will be used as the title, as in:

    # This is my story title
    
    Anything other than headings is ignored.
    
    ```html
    story here
    ```
  • Because titles can't be repeated, multiple code blocks preceded by a single heading will get placeholder titles in the form story @ {file}:{line}.

And that's it! For a working example, see primer-breadcrumb/stories.js and the accompanying README.md.

/cc @primer/ds-core

@broccolini
Copy link
Contributor

broccolini commented Sep 13, 2017

Omg you're hilarious. Yassss! 🎉 Without reviewing the code or reading the mdast docs, would we be able to mark certain stories in the readme that we want pulled in? Asking as I don't think we'd want all of the code snippets within a readme pulled in, though I think we could make that work with some readme improvements. Also, how do they get their name in the sidebar?

Copy link
Member

@jonrohan jonrohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A woohoo 🦆

@shawnbot
Copy link
Contributor Author

This is definitely WIP. Let's discuss tomorrow.

@shawnbot
Copy link
Contributor Author

shawnbot commented Sep 13, 2017

I got a bug in my brain about how to do the titles, and added support for excluding code blocks (because it seemed to me that blacklisting was better than whitelisting). See the updated PR description for how it works! After e02bd53, the Storybook UI shows this:

image

@shawnbot
Copy link
Contributor Author

Okay @broccolini, now primer-breadcrumb is the only module that uses this, and the usage example has title="Breadcrumb" to match what was there previously.

@shawnbot shawnbot merged commit d0ff8cc into dev Sep 15, 2017
@shawnbot shawnbot deleted the stories-from-markdown branch September 15, 2017 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants